/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.allotmanagementpc.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.FndWmMoveTypeBase;
import com.lightkits.common.db.entity.WmExecuteLine;
import com.lightkits.common.db.entity.WmRequestHeader;
import com.lightkits.common.db.entity.WmRequestLine;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.object.ParseUtil;
import com.lightkits.wms.client.DocumentServiceClient;
import com.lightkits.wms.domain.allotmanagementpc.CreateDocumentParamsService;
import com.lightkits.wms.dto.allotmanagementpc.model.ExecuteLinePcDto;
import com.lightkits.wms.dto.allotmanagementpc.model.RequestHeaderPcDto;
import com.lightkits.wms.dto.allotmanagementpc.model.RequestLinePcDto;
import com.lightkits.wms.dto.document.GenerateDocNumRequestType;
import com.lightkits.wms.dto.document.GenerateDocNumResponseType;
import com.lightkits.wms.util.models.Constrants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 魏静
 * @create 2019-07-22 14:47
 * @desc 创建领料单单据参数
 **/
@Service
public class CreateDocumentParamsServiceImpl implements CreateDocumentParamsService {
    private final DocumentServiceClient client;
    private static final String DOC_TYPE = "WR";
    private static final int SIZE = 5;
    private static final int COUNT = 1;
    private static final Long LINE_NUM = 10L;
    private static final String MOVE_TYPE_CODE = "WM_ALLOT";
    private static final String OPERATION_TYPE = "A";
    private static final String LINE_STATUS = "ISSUED";
    private static final String DOCUMENT_TYPE = "WM_REQUEST";

    @Autowired
    public CreateDocumentParamsServiceImpl(DocumentServiceClient client) {
        this.client = client;
    }


    /**
     * 创建领料单参数
     *
     * @param headerPcDto 领料单头参数
     * @return WmRequestHeader
     */
    @Override
    public WmRequestHeader createRequestHeader(RequestHeaderPcDto headerPcDto) {
        WmRequestHeader header = new WmRequestHeader();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        GenerateDocNumRequestType request = new GenerateDocNumRequestType();
        request.setCount(COUNT);
        request.setSize(SIZE);
        request.setType(headerPcDto.getDocumentType() + DOC_TYPE);
        request.setField1(df.format(new Date()));
        GenerateDocNumResponseType response = client.generateDocNumber(request);
        header.setRequestNum(response.getDocNumList().get(0));
        header.setEnterpriseId(ParseUtil.tryLongWithException(headerPcDto.getFromEnterpriseId()));
        header.setToEnterpriseId(ParseUtil.tryLongWithException(headerPcDto.getToEnterpriseId()));
        header.setOrganizationId(ParseUtil.tryLongWithException(headerPcDto.getFromOrganizationId()));
        header.setToOrganizationId(ParseUtil.tryLongWithException(headerPcDto.getToOrganizationId()));
        header.setOperationType(OPERATION_TYPE);
        header.setProcessStatus(headerPcDto.getRequestStatus());
        header.setCreatorId(ParseUtil.tryLongWithException(headerPcDto.getCreatorId()));
        header.setRemark(headerPcDto.getRemark());
        header.setRequestType(headerPcDto.getDocumentType());
        FndWmMoveTypeBase moveTypeBase = new FndWmMoveTypeBase().selectOne(new EntityWrapper<FndWmMoveTypeBase>()
                .eq(FndWmMoveTypeBase.MOVE_TYPE_CODE, MOVE_TYPE_CODE));
        header.setWmMoveTypeId(moveTypeBase.getId());
        return header;
    }

    /**
     * 获取领料单参数
     *
     * @param linePcDtos   领料单行Id
     * @param headerId     开启条码管理的领料单头Id
     * @param headerCopyId 不开启条码管理的头Id
     * @return WmRequestLine
     */
    @Override
    public List<WmRequestLine> createRequestLine(List<RequestLinePcDto> linePcDtos,
                                                 Long headerId,
                                                 Long headerCopyId) {
        List<WmRequestLine> lines = new ArrayList<>();
        Long num = LINE_NUM;
        for (RequestLinePcDto target : linePcDtos) {
            WmRequestLine wmRequestLine = new WmRequestLine();
            if (target.isTagControl()) {
                wmRequestLine.setRequestId(headerId);
            } else {
                wmRequestLine.setRequestId(headerCopyId);
            }

            wmRequestLine.setApplyQty(new BigDecimal(target.getApplyQty()));
            if (!ObjectUtil.isNullOrEmpty(target.getReceiveQty())) {
                wmRequestLine.setConfirmedQty(new BigDecimal(target.getReceiveQty()));
            } else {
                wmRequestLine.setConfirmedQty(BigDecimal.ZERO);
            }
            if (!ObjectUtil.isNullOrEmpty(target.getSendQty())) {
                wmRequestLine.setExeutedQty(new BigDecimal(target.getSendQty()));
            } else {
                wmRequestLine.setExeutedQty(BigDecimal.ZERO);
            }
            wmRequestLine.setItemId(ParseUtil.tryLongWithException(target.getItemId()));
            wmRequestLine.setUom(target.getUomCode());
            wmRequestLine.setLotNumber(target.getLotNumber());
            wmRequestLine.setWarehouseId(ParseUtil.tryLongWithException(target.getFromWarehouseId()));
            wmRequestLine.setToWarehouseId(ParseUtil.tryLongWithException(target.getToWarehouseId()));
            wmRequestLine.setOwnerType(Constrants.OWN);
            wmRequestLine.setLineStatus(LINE_STATUS);
            wmRequestLine.setPickedFlag(false);
            wmRequestLine.setLineNum(num);
            num = num + LINE_NUM;
            lines.add(wmRequestLine);
        }
        return lines;

    }

    /**
     * 创建执行标参数
     *
     * @param exeLinePcDtos 执行表数据
     * @param eventId       事件Id
     * @return WmExecuteLine
     */
    @Override
    public List<WmExecuteLine> createExeLine(List<ExecuteLinePcDto> exeLinePcDtos, Long eventId) {
        List<WmExecuteLine> exeLines = new ArrayList<>();
        exeLinePcDtos.forEach(target -> {
            WmExecuteLine line = new WmExecuteLine();
            line.setDocumentId(ParseUtil.tryLongWithException(target.getRequestHeaderId()));
            line.setDocumentLineId(ParseUtil.tryLongWithException(target.getRequestLineId()));
            line.setDocumentType(DOCUMENT_TYPE);
            line.setEventId(eventId);
            line.setExecutedQty(new BigDecimal(target.getInOfLibQty()));
            line.setExecutedTime(new Date());
            line.setItemId(ParseUtil.tryLongWithException(target.getItemId()));
            line.setUom(target.getUomCode());
            line.setLotNumber(target.getLotNumber());
            line.setWarehouseId(ParseUtil.tryLongWithException(target.getFromWarehouseId()));
            line.setToWarehouseId(ParseUtil.tryLongWithException(target.getToWarehouseId()));
            line.setOwnerType(Constrants.OWN);
            exeLines.add(line);
        });
        return exeLines;
    }
}
